<?php

namespace app\common\model;

use think\Model;

class Post extends Model
{
    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    // 关联评论表
    public function comment()
    {
        return $this->hasMany('comment');
    }
    // 关联图片表
    public function images()
    {
        return $this->belongsToMany('Image','post_image');
    }
    // 关联顶踩表
    public function support()
    {
        return $this->hasMany('support');
    }
    // 关联user表
    public function user()
    {
        return $this->belongsTo('user');
    }
    // 关联文章表
    public function share()
    {
        // 反向
        return $this->belongsTo('Post','share_id','id');
    }

    // 发布文章
    public function createPost()
    {
        // 获取所有参数
        $param = request()->param();
        $userModel = new User();

        //获取userid
        $userId = request()->userId;
        $user = $userModel->find($userId);
        $path = $user->userinfo->path;
        // 发布文章
        $title = mb_substr($param['text'],0,30);
        $post = $this->create([
            'user_id' => $userId,
            'title' => $title,
            'titlepic' => '',
            'content' => $param['text'],
            'path' => $path ? $path : '未知',
            'type' => 0,
            'post_class_id' => $param['post_class_id'],
            'share_id' => 0,
            'isopen' => $param['isopen']
        ]);
        // 截取图片 
        $imglistLength = count($param['imglist']);
        if($imglistLength > 0)
        {
            $ImageModel = new Image();
            for($i = 0 ;$i < $imglistLength; $i++)
            {
                if($ImageModel->isImageExists($param['imglist'][$i]['id'],$userId)){
                    $imgidarr[] = $param['imglist'][$i]['id'];
                }
            }
            // 发布关联
            $post->images()->attach($imgidarr,['create_time' => time()]);
        }
        return true;


    }

    // 获取文章详情
    public function goPostDetail()
    {
        $param  = request()->param();
        $detail = $this->with(['user' => function($query){
            return $query->with(['userinfo'])->field(['id','username','userpic']);
        },'images' => function($query){
            return $query->field('url')->hidden(['pivot']);
        },'share'])->get($param['id']);
        return $detail;
    }   
    // 搜索文章
    public function Search()
    {
        $param = request()->param();
        return $this->where('title','like','%'.$param['keyword'].'%')->with(['user' => function($query){
            return $query->field(['id','username','userpic']);
        },'images' => function($query){
            return $query->field('url')->hidden(['pivot']);
        },'share'])->page($param['page'],10)->select();
    }

    // 获取当前文章下的评论列表
    public function getComment()
    {
        // 获取所有参数
        $param  = request()->param();
        return $this->get($param['id'])->comment()->with(['user' => function($query){
            return $query->field(['username','id','userpic']);
        }])->select();
    }
}
